/**
 * 
 */
package com.mvc.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

import com.alibaba.fastjson.JSON;

/**
 * 生成json模板类
 * @author qingjie
 * Date:2015-11-15
 *
 */
public class CreatHighChartJson {
	
	/**
	 * @author qingjie
	 * @param titleName  标题名称
	 * @param unit 字段单位
	 * @param xAxis x轴数组
	 * @param map y轴
	 */
	public static String CreatJsonData(String titleName, String unit,String yAxixname, String[] xAxis, Map<String, Object> map){
		boolean x = false;
		boolean s = false;
		
		StringBuffer sb = new StringBuffer("{");
		
		//标题拼接
		sb.append("\"title\":{")
		.append("\"text\":")
		.append("\""+titleName+"\",\"align\":\"center\",\"style\":{\"color\":\"#f10\",\"fontSize\":\"16px\"}")
		.append("},");
		
		//x轴数据拼接
		sb.append("\"xAxis\":{")
		.append("\"categories\":[");
		for(String categories : xAxis){
			if(x){
				sb.append(",");
			}
			sb.append("\"").append(categories).append("\"");
			x =  true;
		}
		sb.append("]},");
		
		//y轴名称
		sb.append("\"yAxis\":{")
		.append("\"title\": {")
		.append("\"text\":")
		.append("\""+yAxixname+"\"},")
		.append("\"plotLines\":[{\"value\": 0,\"width\": 1,\"color\": \"#808080\"}]")
		.append("},");
		
		//单位
		sb.append("\"tooltip\":{")
		.append("\"valueSuffix\":")
		.append("\""+unit+"\"")
		.append("},");
		
		sb.append("\"legend\": {\"verticalAlign\": \"top\",\"y\": 20},");
		
		//数据显示
		sb.append("\"series\": [");
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			boolean d = false;
			Map.Entry entry = (Map.Entry)it.next();
			Map<String, Object> m = new HashMap<String, Object>();
			m = (Map<String, Object>) entry.getValue();
			if(s){
				sb.append(",");
			}
			sb.append("{\"name\":").append("\""+m.get("name")+"\",").append("\"data\": [");
			List<Double> l  = (List<Double>) m.get("data");
			for (Double data : l) {
				if(d){
					sb.append(",");
				}
				sb.append(data);
				d =  true;
			}
			sb.append("]}");
			s = true;
		}
		sb.append("]");
		//结束符
		sb.append("}");
		
		return sb.toString();
	}

}
